﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Tool;
using Assets.Scripts.UI.Item;
using UnityEngine;
using UnityEngine.UI;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using Button=UnityEngine.UI.Button;
using Debug = UnityEngine.Debug;
using HorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment;

namespace Assets.Scripts.UI.Panel
{
    public class StudentInfoPanel:UIBase<StudentInfoPanel>
    {

        #region 组件

        public Text NameText; //姓名
        public Text SexText; //性别
        public Text NationText; //民族
        public Text BirthDateText; //出生日期
        public Text BirthplaceText; //出生地
        public Text NativeplaceText; //籍贯
        public Text PoliticText; //政治面貌
        public Text IDCardText; //身份证号
        public Text StudentIdText; //学号
        public Text ClassNameText; //班级名 
        public InputField AddressText; //家庭住址
        public InputField TellNumberText; //手机号
        public InputField QQNumberText; //QQ号
        public InputField SpecialityText; //特长
        public InputField HobbyText; //爱好

        public Image PictureImage; //用户照片   

        public Transform RelativesContent; //亲属
        public Transform StudyExpContent; //学习经历
        public Transform GradeContent; //学习成绩
        public Transform CPContent; //课堂表现
        public Transform RemarkContent; //班主任评语

        public Button ExitButton;//退出的按钮
        public Button TermButton;//学期选择的按钮
        public Button ExportButton;//导出学生信息的按钮
        public Button SubmitButton;//提交的按钮
        public Button StudyAddButton;//学习添加按钮
        public Button RelativeAddButton;//亲属信息按钮

        #endregion

        private List<RelativeInfo> relativeInfos { get; set; }
        private List<StudyExpInfo> studyExpInfos { get; set; }
        private List<StudyInfo> studyInfos { get; set; }
        private List<CPExpInfo> cpExpInfos { get; set; }
        private List<RemarkInfo> remarkInfos { get; set; }

        private StudentInfo studentInfo { get; set; }

        private string picturePath { get; set; }//照片的路径

        public static StudentInfoPanel _Instance;


        protected override UIType UIType
        {
            get { return UIType.Panel;}
        }

        protected override void InitOnDestroy()
        {
            _Instance = null;
        }

        protected override void InitOnAwake()
        {
            _Instance = this;
            AutoFindComponent(transform,true);
            ExitButton.onClick.AddListener(Exit);
            //注册事件
            ExportButton.onClick.AddListener(OnExportButtonClick);
            TermButton.onClick.AddListener(()=>{});
            StudyAddButton.onClick.AddListener(OnStudyAddButtonClick);
            RelativeAddButton.onClick.AddListener(OnRelativeAddButtonClick);
            AddressText.onEndEdit.AddListener(x=>studentInfo.Address=x);
            TellNumberText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x))return;
                studentInfo.Phonenumber = Regex.IsMatch(x, @"^1[3|4|5|8][0-9]\d{4,8}$") ? x : null;
                if (studentInfo.Phonenumber == null)
                {
                    TellNumberText.text = null;
                    UIManager._Instance.ShowTipInfo("请输入正确的手机号");
                }
            });
            QQNumberText.onEndEdit.AddListener(x =>
            {
                if (string.IsNullOrEmpty(x))return;
                studentInfo.QQ = Regex.IsMatch(x, @"^\d{5,12}") ? x : null;
                if (studentInfo.QQ == null)
                {
                    QQNumberText.text = null;
                    UIManager._Instance.ShowTipInfo("请输入正确的QQ号");
                }
            });
            SpecialityText.onEndEdit.AddListener(x=>studentInfo.Speciality=x);
            HobbyText.onEndEdit.AddListener(x=>studentInfo.Hobby=x);
            SubmitButton.onClick.AddListener(OnSubmitButtonClick);
            Init();           
        }


        public void RemoveRelativeInfo(RelativeInfo info)
        {
            relativeInfos.Remove(info);
        }

        public void RemoveStudyExpInfo(StudyExpInfo info)
        {
            studyExpInfos.Remove(info);
        }


        /// <summary>
        /// 提交按钮的点击事件
        /// </summary>
        private void OnSubmitButtonClick()
        {
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            if (!string.IsNullOrEmpty(studentInfo.Birthplace)) dict.Add("birthplace", studentInfo.Birthplace);
            if (!string.IsNullOrEmpty(studentInfo.Nativeplace)) dict.Add("nativeplace", studentInfo.Nativeplace);
            if (!string.IsNullOrEmpty(studentInfo.Address)) dict.Add("address", studentInfo.Address);
            if (!string.IsNullOrEmpty(studentInfo.Phonenumber)) dict.Add("phonenumber", studentInfo.Phonenumber);
            if (!string.IsNullOrEmpty(studentInfo.QQ)) dict.Add("qq", studentInfo.QQ);
            if (!string.IsNullOrEmpty(studentInfo.Hobby)) dict.Add("hobby", studentInfo.Hobby);
            if (!string.IsNullOrEmpty(studentInfo.Speciality)) dict.Add("speciality", studentInfo.Speciality);
            if (relativeInfos != null) dict.Add("relativedata", relativeInfos);
            if (studyExpInfos != null) dict.Add("studyexpdata", studyExpInfos);
            NetManager.Instance.SendRequest(RequestType.SubmitPersonInfo,dict,() =>
            {
                UIManager._Instance.ShowTipInfo("提交成功！！！");
            });
        }


        /// <summary>
        /// 学习添加按钮的事件
        /// </summary>
        private void OnStudyAddButtonClick()
        {
            StudyExpInfo expInfo=new StudyExpInfo();
            Transform tsf = CreateItem(ItemType.StudyExpItem);
            tsf.SetParent(StudyExpContent);
            tsf.localScale=Vector3.one;
            tsf.localPosition=Vector3.zero;
            StudyExpItem item = tsf.GetComponent<StudyExpItem>();
            item.Init(expInfo);
            studyExpInfos.Add(expInfo);
        }

        /// <summary>
        /// 亲属添加按钮的事件
        /// </summary>
        private void OnRelativeAddButtonClick()
        {
            RelativeInfo info=new RelativeInfo();
            Transform tsf = CreateItem(ItemType.RelativeItem);
            tsf.SetParent(RelativesContent);
            tsf.localScale=Vector3.one;
            tsf.localPosition=Vector3.zero;
            RelativeItem item = tsf.GetComponent<RelativeItem>();
            item.Init(info);
            relativeInfos.Add(info);
        }

        /// <summary>
        /// 导出学生信息
        /// </summary>
        private void OnExportButtonClick()
        {
            OpenDialog(WriteToExcel);
        }

        private void Init()
        {
            StudentInfo info=NetReaderFactory.Instance().GetInfoByType<StudentInfo>() as StudentInfo;
            studentInfo = info;
            if (info != null)
            {
                NameText.text = info.Name;
                switch (info.Sex)
                {
                    case 0:
                        SexText.text = "男";
                        break;
                    case 1:
                        SexText.text = "女";
                        break;
                }
                NationText.text = info.Nation;
                BirthDateText.text = info.Brithdate;
                BirthplaceText.text = info.Birthplace;
                NativeplaceText.text = info.Nativeplace;
                PoliticText.text = info.Politic;
                IDCardText.text = info.Idcard;
                StudentIdText.text = info.StudentId;
                ClassNameText.text = info.Class;
                AddressText.text = info.Address;
                TellNumberText.text = info.Phonenumber;
                QQNumberText.text = info.QQ;
                SpecialityText.text = info.Speciality;
                HobbyText.text = info.Hobby;
                StartCoroutine(LoadPicture(info.Headpicpath));
            }
            InitRelativeItem();
            InitStudyExpItem();
            InitCPExpItem();
            InitGradeItem();
            InitRemarkItem();
            if (relativeInfos == null)
            {
                relativeInfos = new List<RelativeInfo>();
                NetReaderFactory.Instance().AddInfoToFactory<List<RelativeInfo>>(relativeInfos);
            }
            if (studyExpInfos == null)
            {
                studyExpInfos = new List<StudyExpInfo>();
                NetReaderFactory.Instance().AddInfoToFactory<List<StudyExpInfo>>(studyExpInfos);
            }
        }       
        
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        IEnumerator LoadPicture(string url)
        {
            if (string.IsNullOrEmpty(url)) yield break;
            Debug.Log("图片的url:" + url);//http://192.168.16.122:8081/upload/08b68297-bff5-4867-a7bb-963a7c4854f4.jpg
            int index =url.LastIndexOf("/", StringComparison.Ordinal);
            string fileName = url.Substring(index);
            picturePath = NetConfig.NET_RES_PATH + fileName;
            DirectoryInfo info = new DirectoryInfo(picturePath);
            if(!info.Exists)//如果加载的路径不存在
            {
                using (WWW www = new WWW(url))
                {
                    yield return www;
                    if (www.isDone && www.error == null)
                    {
                        PictureImage.sprite =
                            Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), Vector2.zero);
                        File.WriteAllBytes(picturePath, EnCriptionTool.Encryption(www.bytes));
                    }
                    else
                    {
                        Debug.Log(www.error);
                    }
                }
            }
            else//存在
            {
                byte[] bytes = File.ReadAllBytes(picturePath);
                Texture2D texture=new Texture2D(1000,1000);
                if (texture.LoadImage(EnCriptionTool.Decryption(bytes)))
                {
                    PictureImage.sprite =
                       Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
                }
            }
        }

        void InitRelativeItem()
        {
            relativeInfos =
               NetReaderFactory.Instance().GetInfoByType<List<RelativeInfo>>() as List<RelativeInfo>;
            if (relativeInfos == null)
            {
                Debug.Log("亲属信息为空");
                return;
            }
            for (int i = 0; i < relativeInfos.Count; i++)
            {
                Transform tsf = CreateItem(ItemType.RelativeItem);
                tsf.SetParent(RelativesContent);
                tsf.localPosition = Vector3.zero;
                tsf.localScale = Vector3.one;
                RelativeItem item = tsf.GetComponent<RelativeItem>();
                item.Init(relativeInfos[i]);
            }    
        }

        void InitCPExpItem()
        {
            cpExpInfos = NetReaderFactory.Instance().GetInfoByType<List<CPExpInfo>>() as List<CPExpInfo>;
            if (cpExpInfos == null)
            {
                Debug.Log("课堂表现信息为空");
                return;
            }
            for (int i = 0; i < cpExpInfos.Count; i++)
            {
                Transform tsf = CreateItem(ItemType.CPExpItem);
                tsf.SetParent(CPContent);
                tsf.localPosition=Vector3.zero;
                tsf.localScale=Vector3.one;
                CPExpItem item = tsf.GetComponent<CPExpItem>();
                item.Init(cpExpInfos[i]);
            }
        }

        void InitStudyExpItem()
        {
            studyExpInfos =
                (List<StudyExpInfo>) NetReaderFactory.Instance().GetInfoByType<List<StudyExpInfo>>();
            if (studyExpInfos == null)
            {
                Debug.Log("学习经历信息为空");
                return;
            }
            for (int i = 0; i < studyExpInfos.Count; i++)
            {
                Transform tsf = CreateItem(ItemType.StudyExpItem);
                tsf.SetParent(StudyExpContent);
                tsf.localPosition = Vector3.zero;
                tsf.localScale = Vector3.one;
                StudyExpItem item = tsf.GetComponent<StudyExpItem>();
                item.Init(studyExpInfos[i]);
            }
        }

        void InitGradeItem()    
        {
            studyInfos =
                (List<StudyInfo>) NetReaderFactory.Instance().GetInfoByType<List<StudentInfo>>();
            if (studyInfos == null)
            {
                Debug.Log("成绩信息为空");
                return;
            }
            for (int i = 0; i < studyInfos.Count; i++)
            {
                Transform tsf = CreateItem(ItemType.CPExpItem);
                tsf.SetParent(GradeContent);
                tsf.localPosition = Vector3.zero;
                tsf.localScale = Vector3.one;
                GradeItem item = tsf.GetComponent<GradeItem>();
                item.Init(studyInfos[i]);
            }
        }

        void InitRemarkItem()
        {
            remarkInfos =
               (List<RemarkInfo>) NetReaderFactory.Instance().GetInfoByType<List<StudentInfo>>();
            if (remarkInfos == null)
            {
                Debug.Log("班主任评语为空");
                return;
            }
            for (int i = 0; i < remarkInfos.Count; i++)
            {
                Transform tsf = CreateItem(ItemType.CPExpItem);
                tsf.SetParent(RemarkContent);
                tsf.localPosition = Vector3.zero;
                tsf.localScale = Vector3.one;
                RemarkItem item = tsf.GetComponent<RemarkItem>();
                item.Init(remarkInfos[i]);
            }
        }

        /// <summary>
        /// 打开window对话框
        /// </summary>
        /// <param name="onSave"></param>
        private void OpenDialog(Action<Stream> onSave)
        {
            using (SaveFileDialog saveFile = new SaveFileDialog
            {
                Title = "保存文件",
                Filter = "Excel files(*.xls)|*.xls|All files(*.*)|*.*",
                InitialDirectory = UnityEngine.Application.dataPath,
                FileName = "studentInfo"        
            })
            {
                try
                {
                    if (saveFile.ShowDialog() == DialogResult.OK)
                    {
                        using (Stream stream = saveFile.OpenFile())
                        {
                            if (onSave != null)
                                onSave(stream);
                        }
                        string savepath = Path.GetDirectoryName(saveFile.FileName);
                        Debug.Log("savepath:" + savepath);
                        Process.Start(savepath);
                    }  
                }
                catch (Exception e)
                {
                    Debug.Log(e.ToString());
                    //throw;
                }  
            }     
        }

        private void WriteToExcel(Stream stream)
        {
            IWorkbook workbook = new HSSFWorkbook();
            //创建表
            ISheet sheet = workbook.CreateSheet("studentInfo");//学生信息表
            int columnWidth = 15*256;//列宽
            int rowHeight = 25;//行高
            //设置列的宽
            for (int i = 0; i <= 8; i++)
            {
                if (i == 5 || i == 7)
                {
                    sheet.SetColumnWidth(i, 30*256);
                }
                else
                {
                    sheet.SetColumnWidth(i, columnWidth);
                }      
            }
            //表头的style
            ICellStyle styleTitle = workbook.CreateCellStyle();
            styleTitle.Alignment = HorizontalAlignment.Center;
            styleTitle.VerticalAlignment=VerticalAlignment.Center;
            IFont font1 = workbook.CreateFont();
            font1.FontHeightInPoints = 20;
            styleTitle.SetFont(font1);

            //表中内容的style
            ICellStyle styleContent = workbook.CreateCellStyle();
            styleContent.Alignment=HorizontalAlignment.Center;
            IFont font2 = workbook.CreateFont();
            font2.FontHeightInPoints = 15;
            styleContent.SetFont(font2);

            //表中较长的内容
            ICellStyle styleLongContent = workbook.CreateCellStyle();
            styleLongContent.Alignment=HorizontalAlignment.Left;
            styleLongContent.WrapText = true;//自动换行
            styleLongContent.VerticalAlignment=VerticalAlignment.Top;
            styleLongContent.SetFont(font2);

            //读取图片
            if (!string.IsNullOrEmpty(picturePath))
            {
                byte[] bytes = File.ReadAllBytes(picturePath);
                int pictureIdx = workbook.AddPicture(bytes, PictureType.PNG);
                // Create the drawing patriarch.  This is the top level container for all shapes.
                IDrawing pariarch = sheet.CreateDrawingPatriarch();
                //添加图片
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 0, 8, 1, 8, 4);
                pariarch.CreatePicture(anchor, pictureIdx);
            }

            #region 初始化信息Test

            ////初始化集合（亲戚关系）
            //relativeInfos = new List<RelativeInfo>();
            //for (int i = 0; i < 5; i++)
            //{
            //    RelativeInfo relativeInfo = new RelativeInfo
            //    {
            //        Name = "丹安安",
            //        Relationship = "亲戚",
            //        PhoneNumber = "1513467644",
            //        Company = "浙江太学"
            //    };
            //    relativeInfos.Add(relativeInfo);
            //}
            ////学习经历
            //studyExpInfos = new List<StudyExpInfo>();
            //for (int i = 0; i < 5; i++)
            //{
            //    StudyExpInfo studyExpInfo = new StudyExpInfo
            //    {
            //        SchoolName = "北京大学",
            //        JobStatus = "校长",
            //        StartTime = "1992/01/01--至今",
            //        Certifier = "陈大师"
            //    };
            //    studyExpInfos.Add(studyExpInfo);
            //}
            ////学习成绩
            //studyInfos = new List<StudyInfo>();
            //for (int i = 0; i < 11; i++)
            //{
            //    StudyInfo studyInfo = new StudyInfo
            //    {
            //        Course = "宇宙大爆炸",
            //        Id = i,
            //        Score = "99.9",
            //        Term = "2017上学期"
            //    };
            //    studyInfos.Add(studyInfo);
            //}
            ////课堂表现
            //cpExpInfos = new List<CPExpInfo>();
            //for (int i = 0; i < 8; i++)
            //{
            //    CPExpInfo cpExpInfo = new CPExpInfo
            //    {
            //        Id = i,
            //        Matter = "上课睡觉，影响老师上课",
            //        Score = -10
            //    };
            //    cpExpInfos.Add(cpExpInfo);
            //}
            ////教师评语
            //remarkInfos = new List<RemarkInfo>();
            //for (int i = 0; i < 10; i++)
            //{
            //    RemarkInfo remarkInfo = new RemarkInfo
            //    {
            //        Remark =
            //            "生活中的你文雅恬静，时常会看到你甜甜的笑容。还记得吗？田径场上当你取得佳绩归来时，你的平静掩饰不住你内心的喜悦，同学们的掌声便是对你最好的奖赏。虽然此前的你直很谦虚，但你为了班级的荣誉，勇敢的走上了竞技场。你的拼搏精神很值得大家敬佩，你的成绩大家有目共睹，老师感谢你为班级所做的一切。作为一名中学生，你注意到了全面发展，却忽略了最主要的一个方面，那就是学业。"
            //    };
            //    remarkInfos.Add(remarkInfo);
            //}

            #endregion
            //学生基本信息
            List<string[]> contents = new List<string[]>
            {
                new []{"学生信息表"},
                new []{ "姓名", NameText.text, "性别", SexText.text, "民族",NationText.text, "出生年月", BirthDateText.text },
                new []{"出生地",BirthplaceText.text,"","","籍贯",NativeplaceText.text,"政治面貌",PoliticText.text},
                new []{ "身份证号", IDCardText.text, "", "", "学号", StudentIdText.text, "班级", ClassNameText.text },
                new []{ "家庭住址",AddressText.text , "", "", "手机",TellNumberText.text, "QQ", QQNumberText.text },
                new []{ "特长",SpecialityText.text },
                new []{"兴趣爱好", HobbyText.text},
                new []{"亲属关系"}
            };
            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 8));
            sheet.AddMergedRegion(new CellRangeAddress(1, 1, 1, 3));
            sheet.AddMergedRegion(new CellRangeAddress(2, 2, 1, 3));
            sheet.AddMergedRegion(new CellRangeAddress(3, 3, 1, 3));
            sheet.AddMergedRegion(new CellRangeAddress(4, 4, 1, 3));
            sheet.AddMergedRegion(new CellRangeAddress(4, 4, 7, 8));
            sheet.AddMergedRegion(new CellRangeAddress(5, 5, 1, 8));
            sheet.AddMergedRegion(new CellRangeAddress(6, 6, 1, 8));
            sheet.AddMergedRegion(new CellRangeAddress(7, 7, 0, 8));
            int line = 8;
            //添加亲属信息
            if (relativeInfos != null)
            {
                for (int i = 0; i < relativeInfos.Count; i++)
                {
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 1, 2));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 4, 5));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 7, 8));
                    contents.Add(new[] { relativeInfos[i].relationship, relativeInfos[i].name, "", "工作单位", relativeInfos[i].company, "", "手机号", relativeInfos[i].phonenumber });
                    line++;
                }
            }
            //添加学习经历
            contents.Add(new[] { "学习经历" });
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 8));
            line++;
            contents.Add(new[] { "起止时间", "", "", "学校名称", "", "任职情况", "", "证明人" });
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 2));
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 3, 4));
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 5, 6));
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 7, 8));
            if (studyExpInfos != null)
            {
                for (int i = 0; i < studyExpInfos.Count; i++)
                {
                    line++;
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 2));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 3, 4));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 5, 6));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 7, 8));
                    contents.Add(new[] { studyExpInfos[i].starttime, "", "", studyExpInfos[i].schoolname, "", studyExpInfos[i].jobstatus, "", studyExpInfos[i].certifier });
                }
            }
            //添加学习成绩
            contents.Add(new []{"学习成绩"});
            line++;
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 8));
            contents.Add(new[] { "学期", "", "", "科目", "", "", "成绩" });
            line++;
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 2));
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 3, 5));
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 6, 8));
            if (studyInfos != null)
            {
                for (int i = 0; i < studyInfos.Count; i++)
                {
                    line++;
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 2));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 3, 5));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 6, 8));
                    contents.Add(new[] { studyInfos[i].Term, "", "", studyInfos[i].Course, "", "", studyInfos[i].Score, "", "" });
                }
            }
            //添加课堂表现
            line++;
            contents.Add(new []{"课堂表现"});
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 8));
            line++;
            contents.Add(new[] { "奖惩（分数）", "", "", "事由" });
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 2));
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 3, 8));
            if (cpExpInfos != null)
            {
                for (int i = 0; i < cpExpInfos.Count; i++)
                {
                    line++;
                    contents.Add(new[] { cpExpInfos[i].Score.ToString(), "", "", cpExpInfos[i].Matter });
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 2));
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 3, 8));
                }
            }
            //添加教师评语
            line++;
            contents.Add(new []{"教师评语"});
            sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 8));
            if (remarkInfos != null)
            {
                for (int i = 0; i < remarkInfos.Count; i++)
                {
                    line++;
                    sheet.AddMergedRegion(new CellRangeAddress(line, line, 0, 8));
                    contents.Add(new[] { remarkInfos[i].Remark });
                }  
            }
            for (int i = 0; i < contents.Count; i++)
            {
                CreateRow(sheet,i,contents[i],styleContent,rowHeight);
            }
            workbook.Write(stream);
        }

        /// <summary>
        /// 创建行
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="line"></param>
        /// <param name="contents"></param>
        /// <param name="cellStyle"></param>
        /// <param name="rowHeight"></param>
        private void CreateRow(ISheet sheet, int line, string[] contents, ICellStyle cellStyle,int rowHeight)
        {
            IRow row = sheet.CreateRow(line);
            row.HeightInPoints = rowHeight;
            for (int i = 0; i < contents.Length; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(contents[i]);
                cell.CellStyle = cellStyle;
            }
        }
    }
}
